﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class administration_listProfiles : System.Web.UI.Page
{
    private const string IDPAGE = "8";

    protected void Page_Load(object sender, EventArgs e)
    {
        //Check the status of the session
        STATE_SESSION();

        //Verify the view that corresponds show
        if (!Page.IsPostBack)
        {
            string flgInsert = Request.Cookies.Get("flgInsert").Value;
            string flgUpdate = Request.Cookies.Get("flgUpdate").Value;
            string flgDelete = Request.Cookies.Get("flgDelete").Value;

            //Disable the checks after loading the datalist
            UpdateInsertDelete(flgUpdate, flgInsert, flgDelete);
            //Built the menu according to user roles
            BuildMenu();
        }
    }

    //Method to verify the status of the current session
    public void STATE_SESSION()
    {
        if (!Page.IsPostBack)
        {
            HttpCookie state = Request.Cookies.Get("state");
            if (state == null)
                Response.Redirect("../index.aspx");
        }
    }

    //Method to verify the user's role
    private void UpdateInsertDelete(string update, string insert, string delete)
    {
        if (!insert.Equals("1"))
        {
            btnAdd.Enabled = false;
        }
        if (!delete.Equals("3"))
        {
            btnRemove.Enabled = false;
        }
    }

    //Method to build menu
    private void BuildMenu()
    {
        string programName = Request.Cookies.Get("programName").Value;
        char[] delimiterChars = { ',' };
        string[] vectorProgramName = programName.Split(delimiterChars);
        string welcome = "";
        string segurity = "";
        string system = "";
        string parameters = "";
        string people = "";
        string operations = "";
        string reports = "";
        for (int i = 0; i < vectorProgramName.Length; i++)
        {
            switch (Convert.ToInt32(vectorProgramName[i]))
            {
                case 1:
                    people += "<li><a class=\"item\" href=\"listNaturalPersons.aspx\"> - Natural Persons</a></li>";
                    break;
                case 2:
                    people += "<li><a class=\"item\" href=\"listLegalPersons.aspx\"> - Legal Persons</a></li>";
                    break;
                case 3:
                    welcome += "<li><a class=\"item\" href=\"index.aspx\"> - Welcome</a></li>";
                    break;
                case 4:
                    parameters += "<li><a class=\"item\" href=\"listCategory.aspx\"> - Category</a></li>";
                    break;
                case 5:
                    system += "<li><a class=\"item\" href=\"listLogTransaction.aspx\"> - Log Transactions</a></li>";
                    break;
                case 6:
                    operations += "<li><a class=\"item\" href=\"listNews.aspx\"> - News</a></li>";
                    break;
                case 7:
                    operations += "<li><a class=\"item\" href=\"listOffers.aspx\"> - Offers</a></li>";
                    break;
                case 8:
                    segurity += "<li><a class=\"item\" href=\"listProfiles.aspx\"> - Profiles</a></li>";
                    break;
                case 9:
                    system += "<li><a class=\"item\" href=\"listPrograms.aspx\"> - Programs</a></li>";
                    break;
                case 10:
                    operations += "<li><a class=\"item\" href=\"listProyects.aspx\"> - Proyects</a></li>";
                    break;
                case 11:
                    segurity += "<li><a class=\"item\" href=\"listRoles.aspx\"> - Roles</a></li>";
                    break;
                case 12:
                    segurity += "<li><a class=\"item\" href=\"listUsers.aspx\"> - Users</a></li>";
                    break;
                case 13:
                    reports += "<li><a class=\"item\"href=\"#\"> - Proyects</a></li>";
                    break;
                case 14:
                    system += "<li><a class=\"item\"href=\"listWorkArea.aspx\"> - WorkArea</a></li>";
                    break;
            }
        }
        lblMenu.Text = "<div class=\"left\"><ul class=\"title\"><li>Administration Menu</li></ul><ul id=\"menu\"><li>";
        lblMenu.Text += "<a href=\"#\">Welcome</a><ul>";
        lblMenu.Text += welcome;
        lblMenu.Text += "</ul></li>";
        lblMenu.Text += "<li><a href=\"#\">Security Management</a><ul>";
        lblMenu.Text += segurity;
        lblMenu.Text += "</ul></li>";
        lblMenu.Text += "<li><a href=\"#\">System Management</a><ul>";
        lblMenu.Text += system;
        lblMenu.Text += "</ul></li>";
        lblMenu.Text += "<li><a href=\"#\">Parameters Management</a><ul>";
        lblMenu.Text += parameters;
        lblMenu.Text += "</ul></li>";
        lblMenu.Text += "<li><a href=\"#\">People Management</a><ul>";
        lblMenu.Text += people;
        lblMenu.Text += "</ul></li>";
        lblMenu.Text += "<li><a href=\"#\">Operations Management</a><ul>";
        lblMenu.Text += operations;
        lblMenu.Text += "</ul></li>";
        lblMenu.Text += "<li><a href=\"#\">Reports</a><ul>";
        lblMenu.Text += reports;
        lblMenu.Text += "</ul></li>";
        lblMenu.Text += "<li><a href=\"../index.aspx\">Out</a></li>";
        lblMenu.Text += "</ul></div>";
    }

    protected void cbRole_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.DataSourceLoad();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (!listboxProfiles.Items.Contains(listboxPrograms.SelectedItem) && (listboxPrograms.SelectedItem != null))
        {
            clsProfile objProfile = new clsProfile();
            objProfile.IdProgram = Convert.ToInt32(listboxPrograms.SelectedValue);
            objProfile.IdRole = Convert.ToInt32(cbRole.SelectedValue);
            objProfile.Insert();
            DataSourceLoad();
            lblMessage.Text = " ";
        }
        else
        {
            lblMessage.Text = "Must make the appropriate selection.";
        }
    }

    private void DataSourceLoad()
    {
        listboxProfiles.DataSourceID = "ODSConsultProfileID";
    }

    protected void btnRemove_Click(object sender, EventArgs e)
    {
        if ((listboxProfiles.SelectedItem != null))
        {
            clsProfile objProfile = new clsProfile();
            objProfile.ID = Convert.ToInt32(listboxProfiles.SelectedValue);
            objProfile.Delete();
            DataSourceLoad();
            lblMessage.Text = " ";
        }
        else
        {
            lblMessage.Text = "Must make the appropriate selection.";
        }
    }
}